/***************************************************************************
 *    
 *      Copyright (c) 2009,2010 KaiTrade LLC (Delaware)
 *                     All Rights Reserved Worldwide
 *
 * STRICTLY PROPRIETARY and CONFIDENTIAL
 *
 * WARNING:  This file is the confidential property of KaiTrade LLC For
 * use only by those with the express written permission and license from
 * KaiTrade LLC.  Unauthorized reproduction, distribution, use or disclosure 
 * of this file or any program (or document) is prohibited. 
 * 
 ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace PlugIn
{
    public class PlugInGeneric : KaiTrade.Interfaces.KTAPlugin
    {
        List<string> m_ControlNames;

        System.Collections.Hashtable m_UserControls;
        private Dictionary<string, System.Drawing.Image> m_ControlImages;
        private PlugInControl m_Control;

        public PlugInGeneric()
        {
            m_UserControls = new System.Collections.Hashtable();
            m_ControlImages = new Dictionary<string, System.Drawing.Image>();
            m_ControlNames = new List<string>();

            // Adding the following controls makes them accessable 
            // from an app that uses the controls as a plugin
            m_Control = new PlugInControl();
            addControl("PlugIn", m_Control);
            m_ControlImages.Add("PlugIn",  PlugIn.Properties.Resources.RO_Mx2_24_circle_colors);

        }

        private void addControl(string myName, System.Windows.Forms.Control myControl)
        {
            m_UserControls.Add(myName, myControl);
            m_ControlNames.Add(myName);
        }

        private System.Windows.Forms.Control getControl(string myName)
        {
            System.Windows.Forms.Control myRet = null;
            if (m_UserControls.ContainsKey(myName))
            {
                myRet = m_UserControls[myName] as System.Windows.Forms.Control;
            }
            return myRet;
        }





        #region KTAPlugin Members

        public System.Windows.Forms.Control CreateUserControl(string myName)
        {
            return this.getControl(myName);
        }

        public KaiTrade.Interfaces.K2Control CreateK2Control(string name)
        {
            return this.getControl(name) as KaiTrade.Interfaces.K2Control;
        }

        /// <summary>
        /// get a list of the controls we support
        /// </summary>
        /// <returns></returns>
        public List<string> GetUserControlName()
        {
            List<string> myNames = new List<string>();
            foreach (string myName in m_ControlNames)
            {
                myNames.Add(myName);
            }
            return myNames;
        }

        public string Name
        {
            get { return "K2SamplePlugIn"; }
        }
        /// <summary>
        /// Get the group name that the control belongs to
        /// </summary>
        public string GroupName { get { return "Main"; } }

        public string VendorName { get { return "KaiTrade LLC"; } }

        /// <summary>
        /// get the controls ID - used to permission
        /// </summary>
        public string ID
        { get { return "AAAABBBB"; } }


      

        public void SetFacade(KaiTrade.Interfaces.Facade myFacade)
        {
            Factory.Instance().AppFacade = myFacade;
        }
        /// <summary>
        /// Set the plugin user context this allows the plugin to identify the current user
        /// </summary>
        /// <param name="myUserContext"></param>
        public void SetUserContext(KaiTrade.Interfaces.User user)
        {
        }

        /// <summary>
        /// This is called after the plugin has been loaded by kaitrade
        /// </summary>
        /// <param name="myState"></param>
        public void Start(string myState)
        {
            try
            {
                m_Control.Start();

                // If you want to register additional extension libraries you can do that here - for example
                // we register an some execution algos below 
                Factory.Instance().AppFacade.Factory.GetAlgoManager().AddInstanceFactory(KaiTrade.Interfaces.AlgoType.ORStrategy, ORAlgoFactory.Instance());
            }
            catch (Exception myE)
            {

            }
        }

        public void Stop()
        {
            try
            {
                m_Control.Stop();
            }
            catch (Exception myE)
            {

            }
        }

       

        public System.Drawing.Image GetControlImage(string myName)
        {
            if (m_ControlImages.ContainsKey(myName))
            {
                return m_ControlImages[myName];
            }
            else
            {
                return Properties.Resources.RO_Mx2_24_circle_colors;

            }
        }

       
        #endregion


        public void SetK2ServiceClient(object svcClient)
        {
            //throw new NotImplementedException();
        }
    }
}
